package com.disease.api.service.web.region;

import com.disease.api.service.entity.Region;
import com.disease.api.service.entity.RegionInfo;
import com.disease.api.service.mapper.RegionInfoMapper;
import com.disease.api.service.model.ResponseResult;
import com.disease.api.service.service.RegionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping(value = "/disease/v1/api/region")
public class RegionController {

    @Autowired
    RegionService regionService;

    @Autowired
    RegionInfoMapper regionInfoMapper;

    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult<List<Region>> queryRegion(@RequestParam("regionName") String address){
        return ResponseResult.success(regionService.queryRegionByName(address));
    }

    @ResponseBody
    @RequestMapping(value = "/queryCatalog", method = RequestMethod.POST)
    public ResponseResult<List<RegionInfo>> queryCatalog(@RequestBody RegionInfo regionInfo){
        if(Optional.ofNullable(regionInfo.getTownsCode()).isPresent()&& !StringUtils.isBlank(regionInfo.getTownsCode())){
            //查询村庄信息
            return ResponseResult.success(regionInfoMapper.queryRegionVillageList(regionInfo));
        }else if(Optional.ofNullable(regionInfo.getCountyCode()).isPresent()&& !StringUtils.isBlank(regionInfo.getCountyCode())){
            //查询乡镇信息
            return ResponseResult.success(regionInfoMapper.queryRegionTownsList(regionInfo));
        }else if(Optional.ofNullable(regionInfo.getCityCode()).isPresent()&& !StringUtils.isBlank(regionInfo.getCityCode())){
            //查询区县信息
            return ResponseResult.success(regionInfoMapper.queryRegionCountryList(regionInfo));
        }else if(Optional.ofNullable(regionInfo.getProvinceCode()).isPresent()&& !StringUtils.isBlank(regionInfo.getProvinceCode())){
            //查询市信息
            return ResponseResult.success(regionInfoMapper.queryRegionCityList(regionInfo));
        }else{
            //查询省信息
            return ResponseResult.success(regionInfoMapper.queryRegionProvinceList());
        }
    }

    /**
     * 查询省
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryProvince", method = RequestMethod.GET)
    public ResponseResult<List<RegionInfo>> queryProvince(){
        return ResponseResult.success(regionInfoMapper.queryRegionProvinceList());
    }

    /**
     * 查询市
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryCity", method = RequestMethod.GET)
    public ResponseResult<List<RegionInfo>> queryCity(@RequestParam("provinceCode") String provinceCode){
        RegionInfo regionInfo = RegionInfo.builder().provinceCode(provinceCode).build();
        return ResponseResult.success(regionInfoMapper.queryRegionCityList(regionInfo));
    }

    /**
     * 获取区县
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryCounty", method = RequestMethod.GET)
    public ResponseResult<List<RegionInfo>> queryCounty(@RequestParam("cityCode") String cityCode){
        RegionInfo regionInfo = RegionInfo.builder().cityCode(cityCode).build();
        return ResponseResult.success(regionInfoMapper.queryRegionCountryList(regionInfo));
    }

    /**
     * 获取乡镇
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryTowns", method = RequestMethod.GET)
    public ResponseResult<List<RegionInfo>> queryTowns(@RequestParam("countryCode") String countryCode){
        RegionInfo regionInfo = RegionInfo.builder().countyCode(countryCode).build();
        return ResponseResult.success(regionInfoMapper.queryRegionTownsList(regionInfo));
    }

    /**
     * 获取村
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryVillage", method = RequestMethod.GET)
    public ResponseResult<List<RegionInfo>> queryVillage(@RequestParam("townsCode") String townsCode){
        RegionInfo regionInfo = RegionInfo.builder().townsCode(townsCode).build();
        return ResponseResult.success(regionInfoMapper.queryRegionVillageList(regionInfo));
    }




}
